﻿using DotNetDBF;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace NyanBankIntfTool
{
    static class Utils
    {
        public static DataSet ReadIniToDataSet(string fileName, string dataSetName = "")
        {
            DataSet dataSet = new DataSet(dataSetName);
            string curTableName = default(string);
            string line;
            DataTable dataTable;

            StreamReader stream = new StreamReader(fileName);
            while ((line = stream.ReadLine()) != null)
            {
                Match match;
                match = Regex.Match(line, @"^\[(.*)\]");
                if (match.Success)
                {
                    GroupCollection groupCollection = match.Groups;
                    curTableName = groupCollection[1].Value;
                    dataTable = new DataTable(curTableName);
                    dataTable.Columns.Add("No", typeof(String));
                    dataTable.Columns.Add("Value", typeof(String));
                    dataSet.Tables.Add(dataTable);
                }

                match = Regex.Match(line, @"^([^\[\];]*?)=([^\[\]]*)$");
                if (match.Success)
                {
                    GroupCollection groupCollection = match.Groups;
                    DataRow row = dataSet.Tables[curTableName].NewRow();
                    row["No"] = groupCollection[1];
                    row["Value"] = groupCollection[2];
                    dataSet.Tables[curTableName].Rows.Add(row);
                }
            }

            return dataSet;
        }

        public static DataSet ReadIniToDataSet2(string fileName, string dataSetName = "")
        {
            DataSet dataSet = new DataSet(dataSetName);
            string curTableName;
            string line;
            DataTable dataTable = null;

            StreamReader stream = new StreamReader(fileName);
            while ((line = stream.ReadLine()) != null)
            {
                Match match;
                match = Regex.Match(line, @"^\[(.*)\]");
                //DataRow row = new DataRow()
                if (match.Success)
                {
                    GroupCollection groupCollection = match.Groups;
                    curTableName = groupCollection[1].Value;
                    dataTable = new DataTable(curTableName);
                    //dataTable.Columns.Add("No", typeof(String));
                    //dataTable.Columns.Add("Value", typeof(String));
                    dataSet.Tables.Add(dataTable);
                    dataTable.Rows.Add();
                }
                else
                {
                    match = Regex.Match(line, @"^([^\[\];]*?)=([^\[\]]*)$");
                    if (match.Success)
                    {
                        GroupCollection groupCollection = match.Groups;
                        dataTable.Columns.Add(groupCollection[1].Value, typeof(string));
                        DataRow row = dataTable.Rows[0];
                        row[groupCollection[1].Value] = groupCollection[2].Value;
                    }
                }
            }

            return dataSet;
        }


        public static Dictionary<string, Dictionary<string, string>> ReadIniToDictionary(string fileName)
        {
            Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>();
            string line;

            StreamReader stream = new StreamReader(fileName);
            string curTableName = "";
            while ((line = stream.ReadLine()) != null)
            {
                Match match;
                match = Regex.Match(line, @"^\[(.*)\]");
                if (match.Success)
                {
                    GroupCollection groupCollection = match.Groups;
                    curTableName = groupCollection[1].Value;
                    ini[curTableName] = new Dictionary<string, string>();
                }
                else
                {
                    match = Regex.Match(line, @"^([^\[\];]*?)=([^\[\]]*)$");
                    if (match.Success)
                    {
                        GroupCollection groupCollection = match.Groups;
                        string key = groupCollection[1].Value;
                        string value = groupCollection[2].Value;
                        ini[curTableName][key] = value;
                    }
                }
            }

            return ini;
        }


        //public static string FindDataInIni(Dictionary<string, Dictionary<string, string>> ini)

        public static DataTable ReadDbfToDataTable(string fileName)
        {
            /*
            DataTable dataTable = new DataTable();
            Table table = Table.Open(fileName);
            dataTable = table.AsDataTable(Encoding.GetEncoding(936));
            return dataTable;
            */
            Stream stream = new FileStream(fileName, FileMode.Open);
            var reader = new DBFReader(stream);
            reader.CharEncoding = Encoding.GetEncoding(936);
            for (int i = 0; i < reader.Fields.Length - 1; i++)
            {
                for (int j = i + 1; j < reader.Fields.Length; j++)
                {
                    if (reader.Fields[i].Name == reader.Fields[j].Name)
                    {
                        reader.Fields[j].Name = $"{reader.Fields[j].Name}2";
                    }
                }
            }

            DataTable dataTable = new DataTable();
            for (int i = 0; i < reader.Fields.Length; i++)
            {
                dataTable.Columns.Add(reader.Fields[i].Name);
            }

            Object[] r;
            while ((r = reader.NextRecord()) != null)
            {
                dataTable.Rows.Add(r);
            }

            reader.Close();
            stream.Close();
            return dataTable;
        }


        public static string FindDataInDataTable(DataTable dataTable, string no)
        {
            foreach (DataRow r in dataTable.Rows)
            {
                if (r["No"] as string == no)
                {
                    return r["Value"] as string;
                }
            }

            return null;
        }

        public static string[] SplitStringByDash(string src)
        {
            return SplitStringByString(src, "-");
        }

        public static string[] SplitStringByComma(string src)
        {
            return SplitStringByString(src, ",");
        }

        public static string[] SplitStringByString(string src, string spliter)
        {
            return src.Split(new string[] {spliter}, StringSplitOptions.None);
        }
    }
}